﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KZ.Express
{
    public partial class CheckPriceView : FormBase, ICheckPriceView
    {
        #region Private Variables

        new CheckPricePresenter _presenter;
        DataTable _supplierCarriers;

        #endregion Private Variables

        #region Private Methods

        private void CheckPriceView_Load(object sender, EventArgs e)
        {
            LoadSupplier();
            //LoadCustomer();
            LoadCountry();
        }

        private void txtWeight_Leave(object sender, EventArgs e)
        {
            int numberDigit = Utility.Info.NumberFormat.NumberDecimalDigits;
            Utility.Info.NumberFormat.NumberDecimalDigits = 1;
            try
            {
                decimal? value = Utility.DecimalNParse(txtWeight.Text);
                txtWeight.Text = Utility.NumberParse(value, Utility.Info);
            }
            finally
            {
                Utility.Info.NumberFormat.NumberDecimalDigits = numberDigit;
            }
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            try
            {
                _presenter.LoadPrice(_supplierCarriers, Utility.IntParse(cmbCustomerID.SelectedValue), Utility.IntParse(cmbCountryID.SelectedValue),
                    Utility.DecimalParse(txtWeight.Text), cmbType.Text, dtpFrom.Value);
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        private void LoadSupplier()
        {
            try
            {
                _supplierCarriers = base._presenter.LoadForCMB<PartnerCarrierENT>(null);
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        private void LoadCustomer()
        {
            try
            {
                SearchCriteria search = new SearchCriteria();
                search.Add("IsCustomer", 1);
                Global.LoadComboBox(cmbCustomerID, base._presenter.LoadForCMB<CustomerENT>(search));
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        private void LoadCountry()
        {
            try
            {
                Global.LoadComboBox(cmbCountryID, base._presenter.LoadForCMB<CountryENT>(null));
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        #endregion

        #region Constructors

        public CheckPriceView()
        {
            InitializeComponent();
            base.InitializeDataGrid(dtgPrice);
            cmbCustomerID.DisplayColumns.Add("PartnerCode", "Mã KH");
            cmbCustomerID.DisplayColumns.Add("PartnerName", "Tên KH");
            cmbCountryID.DisplayColumns.Add("CountryCode", "Mã nước");
            cmbCountryID.DisplayColumns.Add("CountryName", "Tên tên nước");
            cmbCountryID.DropDownWidth = 300;

            this.TopLevel = true;
            base._presenter = _presenter = new CheckPricePresenter(this);
        }

        #endregion

        public DataTable Price
        {
            get { return base._bds.DataSource as DataTable; }
            set { base._bds.DataSource = value; }
        }

        private void dtgPrice_Sorted(object sender, EventArgs e)
        {
            int i = 1;
            foreach (DataGridViewRow row in dtgPrice.Rows)
            {
                row.Cells[colNumber.Index].Value = i++;
            }
        }
    }
}
